﻿using Microsoft.VisualStudio.TestTools.UnitTesting;
using VCDIFF;
using VCDIFF.Rebuild;
using System.Collections.Generic;
using System.Text;
using System;
using System.IO;

namespace VCDIFF.Tests
{
    
    
    /// <summary>
    ///This is a test class for RebuildFileTest and is intended
    ///to contain all RebuildFileTest Unit Tests
    ///</summary>
    [TestClass()]
    public class RebuildFileTest
    {


        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


        /// <summary>
        ///A test for Rebuild
        ///</summary>
        [TestMethod()]
        public void RebuildTest()
        {
            string message = "It is the first time that I type a message on my keyboard.";
            string sourceFileName = "file.txt";
            File.WriteAllText(sourceFileName, message);

            byte[] data = new byte[] {
                // Header
                0xD6,   // V
                0xC3,   // C
                0xC4,   // D
                0x00,   // VCDIFF Version
                0x00,   // Hdr_Indicator : no custom code table, no compression
                0x00,   // Secondary compressor ID
                0x00,   // Length of code table data
                0x00,   // Size of near cache 
                0x00,   // Size of same cache
                // Window
                0x01,       // Win_Indicator
                0x3A,       // Source segment size
                0x00,       // Source segment position
                0x35,       // Length of the delta encoding 
                0x81, 0x0E, // Size of the target window
                0x00,       // Delta_Indicator (no compression)
                0x14,       // Length of data for ADDs and RUNs
                0x18,       // Length of instructions and sizes
                0x03,       // Length of addresses for COPYs
                // Data section for ADDs and RUNs :   \nnot \nIt was a lie !
                0x0A, 0x6E, 0x6F, 0x74, 0x20, 0x0A,
                0x49, 0x74, 0x20, 0x77, 0x61, 0x73, 0x20, 0x61, 0x20, 0x6C, 0x69, 0x65, 0x20, 0x21,                
                // Instructions and sizes section
                0x03, 0x3A, 0x00,   // COPY | Size 58 | No mode
                0x02, 0x01, 0x00,   // RUN  | Size 01 | No mode
                0x03, 0x06, 0x00,   // COPY | Size 06 | No mode
                0x01, 0x04, 0x00,   // ADD  | Size 04 | No mode
                0x03, 0x34, 0x00,   // COPY | Size 52 | No mode
                0x02, 0x01, 0x00,   // RUN  | Size 01 | No mode
                0x01, 0x0D, 0x00,   // ADD  | Size 13 | No mode
                0x02, 0x05, 0x00,   // RUN  | Size 05 | No mode
                // Addresses section for COPYs
                0x00, 0x00, 0x06
            };

            RebuildHeader rebuildHeader = new RebuildHeader();
            Header header = rebuildHeader.Rebuild(data);

            RebuildWindows rebuildWindows = new RebuildWindows();
            IList<Window> windowsList = rebuildWindows.Rebuild(data);

            string targetFileName = "file_modified.txt";
            RebuildFile target = new RebuildFile();
            target.Rebuild(windowsList, sourceFileName);
            target.Write(targetFileName);

            File.WriteAllBytes("file.vcd", data);

            string expected = "It is the first time that I type a message on my keyboard.\n" +
                "It is not the first time that I type a message on my keyboard.\n" +
                "It was a lie !!!!!";

            string actual = File.ReadAllText(targetFileName);

            Assert.AreEqual(expected, actual);
        }

        /// <summary>
        ///A test for RebuildFile Constructor
        ///</summary>
        [TestMethod()]
        public void RebuildFileConstructorTest()
        {
            RebuildFile target = new RebuildFile();
        }
    }
}
